﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Data;
namespace Member
{
	class MySqlLocal
	{
        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

        public MySqlLocal()
        {
            Initialize();
            
        }

        private void Initialize()
        {
            server = "localhost";
            database = "SushibarMember";
            uid = "root";
            password = "";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + 
		    database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            string connectionString2 = "server=" + server + ";database=" + database + ";Character Set=utf8;Uid=" + uid + ";password=" + password + ";Convert Zero Datetime=True;";
            connection = new MySqlConnection(connectionString2);
        }

        public bool OpenConnection()
        {
            try
            {
                connection.Open();
                return true;
            }
            catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        MessageBox.Show("Cannot connect to local server.  Contact administrator");
                        break;

                    case 1045:
                        MessageBox.Show("Invalid username/password, please try again");
                        break;
                    default:
                        MessageBox.Show("Cannot connect to local server.  Contact administrator");
                        break;
                }
                //MessageBox.Show(ex.Message);
                return false;
            }
        }

        public bool CloseConnection()
        {
             try
            {
                connection.Close();
                return true;
            }
            catch (MySqlException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }

        public int ExecuteNonQuery(string query)
        {
            if (this.OpenConnection() == true)
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                int rowsAffected = cmd.ExecuteNonQuery();
                this.CloseConnection();
                return rowsAffected;
            }
            return 0;
        }

        public bool isConnectAvailable()
        {
            if (OpenConnection())
            {
                CloseConnection();
                return true;
            }
            return false;
        }

        public DataTable ExecuteQuery(string query)
        {
            DataTable dt = null;
            if (this.OpenConnection() == true)
            {
                dt = new DataTable();
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                dt.Load(dataReader);
                dataReader.Close();
                this.CloseConnection();
            }
            else
            {
                MessageBox.Show("Cannot connect to the local Server !");
            }
            return dt;
        }
    }
}
